package com.moly.grpc.server.studentmessage;

import com.moly.grpc.studentmessage.Result;
import com.moly.grpc.studentmessage.Sport;
import com.moly.grpc.studentmessage.Student;
import com.moly.grpc.studentmessage.StudentServiceGrpc;
import io.grpc.stub.StreamObserver;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashSet;

import java.util.Random;
import java.util.Set;

public class StudentServiceImpl extends StudentServiceGrpc.StudentServiceImplBase {
    @Override
    public void getResult(Student request, StreamObserver<Result> responseObserver) {

        System.out.println("age  " + request.getAge());
        System.out.println("name  " + request.getName());


        responseObserver.onNext(createRandomResult());

        responseObserver.onCompleted();

    }

    @Override
    public void getStreamResult(Student request, StreamObserver<Result> responseObserver) {


        System.out.println(request.toString());


        responseObserver.onNext(createRandomResult());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        responseObserver.onNext(createRandomResult());


        responseObserver.onCompleted();

    }


    @Override
    public StreamObserver<Student> getResultByStreamRequest(StreamObserver<Result> responseObserver) {

        return new StreamObserver<Student>() {
            @Override
            public void onNext(Student value) {
                System.out.println(value.toString());
                System.out.println(LocalDateTime.now());
            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onCompleted() {
                System.out.println("onCompleted begin");
                responseObserver.onNext(createRandomResult());
                System.out.println("onCompleted end");
                responseObserver.onCompleted();
            }
        };
    }


    @Override
    public StreamObserver<Student> getStreamResultByStreamRequest(StreamObserver<Result> responseObserver) {


        return new StreamObserver<Student>() {
            @Override
            public void onNext(Student value) {
                System.out.println(LocalDateTime.now()+"   客户端发送  "+value.toString());
                responseObserver.onNext(createRandomResult());
            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onCompleted() {
                responseObserver.onCompleted();

            }
        };
    }

    private Result createRandomResult() {

        Sport basketball = Sport.newBuilder().setName("篮球" + new Random().nextFloat()).build();
        Sport pingpang = Sport.newBuilder().setName("乒乓球").build();
        Set<Sport> sports = new HashSet<>();
        sports.add(pingpang);
        sports.add(basketball);

        return Result.newBuilder().setIsMan(true).addAllSport(sports).build();
    }
}
